package com.springboot.controller;

import com.springboot.entity.UserInfoEntity;
import com.springboot.service.UserInfoService;
import com.springboot.utils.ClassUtils;
import com.springboot.utils.CollectionUtil;
import com.springboot.utils.SpringUtils;
import com.springboot.service.anno.DiyScheduled;
import com.springboot.service.job.QuartzManager;
import lombok.extern.apachecommons.CommonsLog;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import sun.rmi.runtime.Log;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * @author Administrator
 */
@RestController
@CommonsLog
public class JobController {
    @Autowired
    private QuartzManager quartzManager;

    @Autowired
    private UserInfoService userInfoService;

    private final String packageName = "com.springboot.service.task";

    @GetMapping("start/test1")
    public void test1() {
        List<UserInfoEntity> list = userInfoService.selectList();
        log.info(list.toString());
    }

    //    @GetMapping("start/test2")
    @RequestMapping(value = "start/test2", method = RequestMethod.GET, produces = "application/json;charset=UTF-8, text/plain;charset=UTF-8")
    private void test2() {

        log.info(getList().toString());
    }

    private List<UserInfoEntity> getList() {
        List<UserInfoEntity> list = userInfoService.selectList();
        return list;
    }


    @GetMapping("start/job1")
    public void startJob1() {
        Set<Class<?>> classSet = ClassUtils.getClassSet(packageName);
        if (!CollectionUtil.isEmpty(classSet)) {
            for (Class<?> aClass : classSet) {
                if (aClass.isAnnotationPresent(DiyScheduled.class)) {
                    Method[] methods = aClass.getDeclaredMethods();
                    for (Method method : methods) {
                        if (method.isAnnotationPresent(DiyScheduled.class)) {
                            DiyScheduled annotation = method.getAnnotation(DiyScheduled.class);
                            String cron = annotation.cron();
                            String taskCode = annotation.taskCode();
                            String className = aClass.getSimpleName();
                            Object taskObj = null;
                            log.info(className);
                            /*try {

                                taskObj = SpringUtils.getBean(aClass);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            if (taskObj == null) {

                                System.out.println(className + "类未找到bean实例");
                                continue;
                            }

                            try {
                                quartzManager.startJob(cron, taskCode, className, taskObj, method);
                            } catch (SchedulerException e) {
                                e.printStackTrace();
                            }*/
                        }
                    }

                    System.out.println(aClass.getName());
                }
            }
        }

        /*Object testTask = SpringUtils.getBean("testTask");
        Method[] declaredMethods = testTask.getClass().getDeclaredMethods();
        for (Method method : declaredMethods) {
            if (method.isAnnotationPresent(DiyScheduled.class)) {
                DiyScheduled annotation = method.getAnnotation(DiyScheduled.class);
                String cron = annotation.cron();
                String name = annotation.taskName();
                String groupName = annotation.beanName();
                try {
                    quartzManager.startJob(cron, name, groupName, testTask, method);
                } catch (SchedulerException e) {
                    e.printStackTrace();
                }
            }
        }*/


    }

    @GetMapping("stop/job1")
    public void stopJob1() {
        quartzManager.stopJob();
    }

    public static void main(String[] args) {
        String str = "hello__world_adbkbds";
        String[] s = str.split("_", 2);
        System.out.println(Arrays.toString(s));
    }


}
